<!-- 维修详情 》 工单记录 -->

<template>
  <div>
    <div v-if="!noDeal" class="DetailRepair-RepairRecord-deal">
      <BtnText v-if="hasHanten" title="加急" @click="openDeal('HASTEN')" />
      <BtnText v-if="hasNote" title="追记" @click="openDeal('NOTE')" />
    </div>

    <EasyScroll full>
      <el-timeline>
        <el-timeline-item v-for="(item, i) in recordList" :key="item.id" :timestamp="CORE.timeStr(item.handleTime)">
          <div class="DetailRepair-RepairRecord-title">{{ item.eventName }}</div>

          <div v-for="(text, i) in getText(item)" :key="i">
            {{ text[0] }}：<span :class="text[2]">{{ text[1] }}</span>
          </div>

          <ShowImage
            v-if="!CORE.isEmpty(item.workOrderEventFileDto)"
            :one="item"
            bind="workOrderEventFileDto"
            pathKey="fileUrl"
            :square="60"
            :gap="4"
            noLabel
          />

          <div v-if="item.eventCode === 'evaluate_task'">
            <ShowRate :one="item" label="服务效率" bind="starNumber" :labelWidth="68" mini />
            <ShowRate :one="item" label="服务质量" bind="starNumberQuality" :labelWidth="68" mini />
            <ShowSlot noLabel mini>
              <span>评价内容：{{ item.opinion || '-' }}</span>
            </ShowSlot>
          </div>

          <template #dot>{{ i + 1 }}</template>
        </el-timeline-item>
      </el-timeline>
    </EasyScroll>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import props from './props'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'RepairRecord',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb(), CORE.mixWeb('deal', 'deal_')],

  /**
   * 属性注册 (抽取以便查阅)
   */
  props,

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 记录列表
     */
    recordList() {
      return this.one.workOrderEventDto || []
    },

    /**
     * 有无加急
     */
    hasHanten() {
      const { status, hastenStatus } = this.one
      if (!status || hastenStatus > 1) return false
      if (['task_draft', 'task_complete', 'task_cancel'].includes(status)) return false
      return true
    },

    /**
     * 有无追记
     */
    hasNote() {
      const { status } = this.one
      if (!status) return false
      if (['task_draft', 'task_complete', 'task_cancel'].includes(status)) return false
      return true
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 打开处理弹窗
     */
    async openDeal(DEAL) {
      const { one, MAP, deal_openEdit } = this
      deal_openEdit({ workOrderId: one.id, ...MAP[DEAL] })
    },

    /**
     * 获取文本
     */
    getText(item) {
      const {
        handleUserName = '',
        handleUserPhone = '',
        assignUserName = '',
        assignUserPhone = '',
        reviewStatus,
        eventCode,
        opinion,
        handleDurationDesc,
        hangEndTime,
      } = item

      const user = `${handleUserName}${handleUserPhone ? ` (${handleUserPhone})` : ''}`
      const worker = `${assignUserName}${assignUserPhone ? ` (${assignUserPhone})` : ''}`
      const review = reviewStatus === 1 ? '同意' : '不同意'
      const color = `DetailRepair-RepairRecord-${reviewStatus === 1 ? 'yes' : 'no'}`

      switch (eventCode) {
        case 'draft_task': // 工单创建
          return [['创建人', user]]

        case 'assign_task': // 工单指派
          return [
            ['操作人', user],
            ['派单人', worker],
          ]

        case 'receive_task': // 工单接收
          return [
            ['接单人', user],
            ['接单意见', opinion],
          ]

        case 'process_task': // 处理完成
          return [
            ['处理人', user],
            ['处理意见', opinion],
            ['处理用时', handleDurationDesc],
          ]

        case 'back_task': // 工单回退
          return [
            ['回退人', user],
            ['回退意见', opinion],
          ]

        case 'forward_task': // 工单转派
          return [
            ['操作人', user],
            ['转派人', worker],
            ['转派意见', opinion],
          ]

        case 'suspend_task': // 工单挂起
          return [
            ['挂起人', user],
            ['预计解挂时间', CORE.dateStr(hangEndTime)],
            ['挂起意见', opinion],
          ]

        case 'resume_task': // 工单解挂
          return [
            ['操作人', user],
            ['解挂意见', opinion],
          ]

        case 'review_task': // 工单审核
          return [
            ['审核人', user],
            ['审核结果', review, color],
            ['审核意见', opinion],
          ]

        case 'suspend_review_task': // 挂起审批
          return [
            ['审批人', user],
            ['审批结果', review, color],
            ['审批意见', opinion],
          ]

        case 'cancel_task': // 工单撤销
          return [
            ['操作人', user],
            ['撤销意见', opinion],
          ]

        case 'hasten_task': // 工单加急
          return [
            ['操作人', user],
            ['加急描述', opinion],
          ]

        case 'add_note_task': // 工单追记
          return [
            ['操作人', user],
            ['追记描述', opinion],
          ]

        case 'cooperate_task': // 工单协同
          return [
            ['操作人', user],
            ['协同人', worker],
            ['协同意见', opinion],
          ]

        case 'cooperate_complete_task': // 协同完成
          return [
            ['协同人', user],
            ['协同意见', opinion],
          ]

        default:
          return []
      }
    },
  },
}
</script>

<style lang="scss">
@import './index.scss';
</style>
